package com.yunzhi.util;

import com.edais.util.DES3Util;
import com.yunzhi.domain.ResultDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Response工具类
 * Created by haoyx on 2017/2/7.
 */
public class ResponseUtil {

    private static final Logger logger = Logger.getLogger(ResponseUtil.class);

    public static PrintWriter getTextWriter(HttpServletResponse response) throws IOException {
        response.setContentType("text/plain; charset=UTF-8");
        return response.getWriter();
    }

    public static PrintWriter getJsonOut(HttpServletResponse response) throws IOException {
        response.setContentType("text/json; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, Cookie");
        return response.getWriter();
    }

    public static void write(HttpServletResponse response, ResultDTO resultDTO) {
        try {
            PrintWriter out = getTextWriter(response);
            String json = resultDTO.toJsonString();
            logger.info("response-" + json);
            out.print(encode(json));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, String value) {
        try {
            PrintWriter out = getTextWriter(response);
            logger.info("response-" + value);
            out.print(encode(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeNotEncode(HttpServletResponse response, String value) {
        try {
            PrintWriter out = getTextWriter(response);
            logger.info("response-" + value);
            out.print(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, JSONObject value) {
        try {
            PrintWriter out = getJsonOut(response);
            String valueStr = value.toString();
            logger.info("response-" + valueStr);
            out.print(encode(valueStr));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void write(HttpServletResponse response, JSONArray value) {
        try {
            PrintWriter out = getJsonOut(response);
            String valueStr = value.toString();
            logger.info("response-" + valueStr);
            out.print(encode(value.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String encode(String result) {
        try {
            return DES3Util.encode(result);
        } catch (Exception e) {
            logger.error("加密失败", e);
        }
        return "";
    }
}
